package com.tanhua.dubbo.api;

import com.tanhua.dubbo.mapper.PeachMapper;
import com.tanhua.dubbo.mapper.UserInfoMapper;
import com.tanhua.model.domain.Peach;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PeachVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Random;

@DubboService
public class PeachApiImpl implements PeachApi {
    @Autowired
    private PeachMapper peachMapper;

    @Override
    public void save(String soundUrl, Long posterId) {
        Peach peach = new Peach();
        peach.setSoundUrl(soundUrl);
        peach.setPosterId(posterId);
        peach.setPickingOrNot(0);
        peachMapper.insert(peach);
    }



    @Override
    public Peach getRandomOne() {
        Integer count = peachMapper.selectCount(null);
        Random random = new Random();
        Integer rand = random.nextInt((count) + 1);
        Peach peach = peachMapper.selectById(rand);
        if (peach!=null&&peach.getPickingOrNot()==0){
            peach.setPickingOrNot(1);
        }else {
           return getRandomOne();
        }
        return peach;
    }

    @Override
    public void update(Peach peach) {
        peachMapper.updateById(peach);
    }
}
